//
// Copyright 2020 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

// Selector '.mdc-*' should only be used in this project.
// stylelint-disable selector-class-pattern --
// Internal styling for Tooltip MDC component.

@use 'sass:map';
@use 'sass:meta';
@use '@material/elevation/elevation-theme';
@use '@material/feature-targeting/feature-targeting';
@use '@material/rtl/rtl';
@use '@material/shape/mixins' as shape-mixins;
@use '@material/button/button-text-theme';
@use '@material/theme/theme';
@use '@material/theme/theme-color';
@use '@material/theme/keys';
@use '@material/typography/typography';
@use '@material/tokens/resolvers';

$fill-color: surface;
$text-color: on-surface;
$divider-color: rgba(theme-color.prop-value(on-surface), 0.12);
$graphic-background-color: primary;
$graphic-color: surface;
$graphic-shape-radius: 50%;
$primary-action-text-color: primary;
$secondary-action-text-color: primary;

$mobile-breakpoint: 480px;
$custom-property-prefix: 'banner';

$light-theme: (
  action-focus-state-layer-color: theme-color.$primary,
  action-focus-state-layer-opacity: 0.12,
  action-focus-label-text-color: null,
  action-hover-state-layer-color: theme-color.$primary,
  action-hover-state-layer-opacity: 0.04,
  action-hover-label-text-color: null,
  action-label-text-color: theme-color.$primary,
  // TODO(b/197004146): Support action label typography.
  action-label-text-font: null,
  action-label-text-size: null,
  action-label-text-tracking: null,
  action-label-text-weight: null,
  action-pressed-state-layer-color: theme-color.$primary,
  action-pressed-state-layer-opacity: 0.1,
  action-pressed-label-text-color: null,
  container-color: theme-color.$surface,
  container-elevation: null,
  container-shadow-color: null,
  container-shape: 0,
  content-max-width: 720px,
  divider-color: $divider-color,
  divider-height: 1px,
  supporting-text-color: theme-color.$on-surface,
  supporting-text-font: typography.get-font(body2),
  supporting-text-line-height: typography.get-line-height(body2),
  supporting-text-size: typography.get-size(body2),
  supporting-text-tracking: typography.get-tracking(body2),
  supporting-text-weight: typography.get-weight(body2),
  with-image-image-shape: $graphic-shape-radius,
  with-image-image-size: 40px,
  with-image-image-color: $graphic-color,
  with-image-image-container-color: $graphic-background-color,
  z-index: 1,
);

@mixin theme($theme, $resolvers: resolvers.$material) {
  // TODO(b/251881053): Replace with `validate-theme`.
  @include theme.validate-theme-styles($light-theme, $theme);
  $theme: _resolve-theme($theme, $resolvers);
  @include keys.declare-custom-properties(
    $theme,
    $prefix: $custom-property-prefix
  );
}

@function _resolve-theme($theme, $resolvers) {
  @return map.merge(
    $theme,
    _resolve-theme-elevation(
      $theme,
      map.get($resolvers, elevation),
      container-elevation
    )
  );
}

@function _resolve-theme-elevation($theme, $resolver, $keys...) {
  @if $resolver == null {
    @return $theme;
  }

  @each $key in $keys {
    // Resolve the value for each state key.
    $resolved-value: meta.call(
      $resolver,
      $elevation: map.get($theme, $key),
      $shadow-color: map.get($theme, container-shadow-color)
    );

    // Update the theme with the resolved value.
    $theme: map.set($theme, $key, $resolved-value);
  }

  @return $theme;
}

@mixin theme-styles(
  $theme,
  $resolver: resolvers.$material,
  $query: feature-targeting.all()
) {
  @include theme.validate-theme-styles($light-theme, $theme);
  $theme: keys.create-theme-properties(
    $theme,
    $prefix: $custom-property-prefix
  );

  @include z-index(map.get($theme, 'z-index'), $query: $query);

  @include max-width(map.get($theme, 'content-max-width'), $query: $query);

  @include graphic-color(
    map.get($theme, 'with-image-image-color'),
    $query: $query
  );
  @include graphic-background-color(
    map.get($theme, 'with-image-image-container-color'),
    $query: $query
  );

  @include text-color(map.get($theme, supporting-text-color), $query: $query);
  @include _supporting-text-typography(
    (
      font: map.get($theme, supporting-text-font),
      size: map.get($theme, supporting-text-size),
      tracking: map.get($theme, supporting-text-tracking),
      weight: map.get($theme, supporting-text-weight),
      line-height: map.get($theme, supporting-text-line-height),
    ),
    $query: $query
  );

  @include graphic-shape-radius(
    map.get($theme, with-image-image-shape),
    $query: $query
  );
  @include _graphic-size(
    map.get($theme, with-image-image-size),
    $query: $query
  );

  @include fill-color(map.get($theme, container-color), $query: $query);
  @include divider-color(map.get($theme, divider-color), $query: $query);
  @include _divider-height(map.get($theme, divider-height), $query: $query);
  @include _banner-shape(map.get($theme, container-shape), $query: $query);
  @include _banner-elevation(
    $resolver,
    map.get($theme, container-elevation),
    map.get($theme, container-shadow-color),
    $query
  );

  .mdc-button {
    @include button-text-theme.theme-styles(
      (
        focus-label-text-color: map.get($theme, action-focus-label-text-color),
        focus-state-layer-color: map.get($theme, action-focus-state-layer-color),
        focus-state-layer-opacity:
          map.get($theme, action-focus-state-layer-opacity),
        hover-label-text-color: map.get($theme, action-hover-label-text-color),
        hover-state-layer-color: map.get($theme, action-hover-state-layer-color),
        hover-state-layer-opacity:
          map.get($theme, action-hover-state-layer-opacity),
        label-text-color: map.get($theme, action-label-text-color),
        label-text-font: map.get($theme, action-label-text-font),
        label-text-size: map.get($theme, action-label-text-size),
        label-text-tracking: map.get($theme, action-label-text-tracking),
        label-text-weight: map.get($theme, action-label-text-weight),
        pressed-label-text-color:
          map.get($theme, action-pressed-label-text-color),
        pressed-state-layer-color:
          map.get($theme, action-pressed-state-layer-color),
        pressed-state-layer-opacity:
          map.get($theme, action-pressed-state-layer-opacity),
      ),
      $query: $query
    );
  }
}

///
/// Customizes fill color.
/// @param {Color | String} $color Either a valid color value or a key from
///     `$theme-theme-color.property-values`.
///
@mixin fill-color($color, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  @include feature-targeting.targets($feat-color) {
    @include theme.property(background-color, $color);
  }

  .mdc-banner__fixed {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(background-color, $color);
    }
  }
}

///
/// Customizes text color.
/// @param {Color | String} $color Either a valid color value or a key from
///     `$theme-theme-color.property-values`.
///
@mixin text-color($color, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-banner__text {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(color, $color);
    }
  }
}

///
/// Customizes divider color.
/// @param {Color | String} $color Either a valid color value or a key from
///     `$theme-theme-color.property-values`.
///
@mixin divider-color($color, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  @include feature-targeting.targets($feat-color) {
    @include theme.property(border-bottom-color, $color);
  }

  .mdc-banner__fixed {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(border-bottom-color, $color);
    }
  }
}

@mixin _divider-height($height, $query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  @include feature-targeting.targets($feat-structure) {
    @include theme.property(border-bottom-width, $height);
  }

  .mdc-banner__fixed {
    @include feature-targeting.targets($feat-structure) {
      @include theme.property(border-bottom-width, $height);
    }
  }
}

///
/// Customizes the graphic color.
/// @param {Color | String} $color Either a valid color value or a key from
///     `$theme-theme-color.property-values`.
///
@mixin graphic-color($color, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-banner__graphic {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(color, $color);
    }
  }
}

///
/// Customizes the graphic background color.
/// @param {Color | String} $color Either a valid color value or a key from
///     `$theme-theme-color.property-values`.
///
@mixin graphic-background-color($color, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-banner__graphic {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(background-color, $color);
    }
  }
}

///
/// Customizes the graphic shape radius.
/// @param {Number} $shape-radius Shape radius in length or percentage.
///
@mixin graphic-shape-radius($shape-radius, $query: feature-targeting.all()) {
  .mdc-banner__graphic {
    @include shape-mixins.radius($shape-radius, $query: $query);
  }
}

///
/// Sets the min-width for the banner content.
/// @param {Number} $min-width Minimum width value in `px`.
/// @param {Number} $mobile-breakpoint Mobile breakpoint value in `px`.
///
@mixin min-width(
  $min-width,
  $mobile-breakpoint: $mobile-breakpoint,
  $query: feature-targeting.all()
) {
  $feat-structure: feature-targeting.create-target($query, structure);
  .mdc-banner__content,
  .mdc-banner__fixed {
    @include feature-targeting.targets($feat-structure) {
      min-width: $min-width;

      // The first media query ensures that banners are always 100% width on
      // mobile devices, as required by the spec. The second media query
      // prevents banners from being wider than the viewport for large min-width
      // values.
      @media (max-width: $mobile-breakpoint), (max-width: $min-width) {
        min-width: 100%;
      }
    }
  }
}

///
/// Sets the max-width for the banner content.
/// @param {Number} $max-width Maximum width value in `px`.
///
@mixin max-width($max-width, $query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  .mdc-banner__content {
    @include feature-targeting.targets($feat-structure) {
      @include theme.property(max-width, $max-width);
    }
  }
}

///
/// Sets the banner content to centered instead of leading.
///
@mixin position-centered($query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  .mdc-banner__content {
    @include feature-targeting.targets($feat-structure) {
      left: 0;
      margin-left: auto;
      margin-right: auto;
      right: 0;
    }
  }
}

///
/// Sets the banner content to stacked layout.
///
@mixin layout-stacked($query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  @include feature-targeting.targets($feat-structure) {
    .mdc-banner__content {
      flex-wrap: wrap;
    }

    .mdc-banner__graphic {
      margin-bottom: 12px;
    }

    .mdc-banner__text {
      @include rtl.reflexive-property(margin, 16px, 8px);
      padding-bottom: 4px;
    }

    .mdc-banner__actions {
      margin-left: auto;
    }
  }
}

// Sets the z-index of the banner.
// @param {Number} $z-index
@mixin z-index($z-index, $query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  @include feature-targeting.targets($feat-structure) {
    @include theme.property(z-index, $z-index);
  }
}

// Sets the width of the banner fixed element. Use to adjust the width of the
// fixed banner in cases where width is not the same as the viewport.
// @param {Number} $width Width value in `px`.
@mixin fixed-width($width, $query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  .mdc-banner__fixed {
    @include feature-targeting.targets($feat-structure) {
      width: $width;
    }
  }
}

@mixin _supporting-text-typography(
  $typography-map,
  $query: feature-targeting.all()
) {
  $feat-typography: feature-targeting.create-target($query, typography);

  $font: map.get($typography-map, font);
  $size: map.get($typography-map, size);
  $tracking: map.get($typography-map, tracking);
  $weight: map.get($typography-map, weight);
  $line-height: map.get($typography-map, line-height);

  .mdc-banner__text {
    @include feature-targeting.targets($feat-typography) {
      @include theme.property(letter-spacing, $tracking);
      @include theme.property(font-size, $size);
      @include theme.property(font-family, $font);
      @include theme.property(font-weight, $weight);
      @include theme.property(line-height, $line-height);
    }
  }
}

@mixin _banner-shape($shape, $query: feature-targeting.all()) {
  @include shape-mixins.radius($shape, $query: $query);
}

@mixin _banner-elevation(
  $resolver,
  $elevation,
  $shadow-color,
  $query: feature-targeting.all()
) {
  $elevation-resolver: map.get($resolver, elevation);

  @include elevation-theme.with-resolver(
    $elevation-resolver,
    $elevation: $elevation,
    $shadow-color: $shadow-color,
    $query: $query
  );
}

@mixin _graphic-size($size, $query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);
  .mdc-banner__graphic {
    @include feature-targeting.targets($feat-structure) {
      @include theme.property(height, $size);
      @include theme.property(width, $size);
    }
  }
}
